<template>
  <div class="repair-manage-container">
    <el-card>
      <div class="filter-bar">
        <el-select v-model="filterStatus" placeholder="按状态筛选" @change="fetchData">
          <el-option label="全部" value="" />
          <el-option label="待审核" value="待审核" />
          <el-option label="处理中" value="处理中" />
          <el-option label="已完成" value="已完成" />
        </el-select>
      </div>

      <el-table
        :data="repairList"
        stripe
        border
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="baoxiuleixing" label="类型" />
        <el-table-column prop="xiangqingmiaoshu" label="描述" />
        <el-table-column prop="sushehao" label="宿舍" />
        <el-table-column prop="loudonghao" label="楼栋" />
        <el-table-column prop="addtime" label="提交时间" width="180" />
        <el-table-column prop="sfsh" label="状态" width="100" />
        <el-table-column label="操作" width="220">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">处理</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      />
    </el-card>

    <el-dialog v-model="dialogVisible" title="处理报修" width="600px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="ID">
          <el-input v-model="form.id" disabled />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.xiangqingmiaoshu" type="textarea" disabled />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.sfsh" placeholder="请选择处理状态">
            <el-option label="待审核" value="待审核" />
            <el-option label="处理中" value="处理中" />
            <el-option label="已完成" value="已完成" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitUpdate">确认处理</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { repairAPI } from '@/utils/api'

const repairList = ref([])
const loading = ref(false)
const filterStatus = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const form = reactive({
  id: null,
  baoxiubianhao: '',
  xiangqingmiaoshu: '',
  sfsh: ''
})
const multipleSelection = ref([])


const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      page: currentPage.value,
      limit: pageSize.value
    }
    if (filterStatus.value) {
      params.sfsh = filterStatus.value
    }
    const res = await repairAPI.getList(params)
    if (res.code === 0) {
      repairList.value = res.data.list
      total.value = res.data.total
      pageSize.value = res.data.pageSize
      currentPage.value = res.data.currPage
    }
  } finally {
    loading.value = false
  }
}

const handlePageChange = (page) => {
  currentPage.value = page
  fetchData()
}

const handleEdit = (row) => {
  form.id = row.id
  form.baoxiubianhao = row.baoxiubianhao
  form.xiangqingmiaoshu = row.xiangqingmiaoshu
  form.sfsh = row.sfsh || '待审核'
  dialogVisible.value = true
}

const submitUpdate = async () => {
  try {
    const res = await repairAPI.update(form)
    if (res.code === 0) {
      ElMessage.success('处理成功')
      dialogVisible.value = false
      fetchData()
    } else {
      ElMessage.error(res.msg || '处理失败')
    }
  } catch (e) {
    console.error(e)
    ElMessage.error('处理失败')
  }
}

const handleSelectionChange = (val) => {
  multipleSelection.value = val.map(item => item.id)
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该条报修记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await repairAPI.delete([row.id])
      if (res.code === 0) {
        ElMessage.success('删除成功')
        fetchData()
      } else {
        ElMessage.error(res.msg || '删除失败')
      }
    } catch (err) {
      console.error(err)
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.repair-manage-container {
  padding: 20px;
}
.filter-bar {
  margin-bottom: 20px;
}
</style>